package com.avcdata.aoscustommade.model;

import android.content.Context;

import com.avcdata.aoscustommade.bean.BaseResponseBean;
import com.avcdata.aoscustommade.bean.GetVerificationCodeBean;
import com.avcdata.aoscustommade.bean.VerifyNoteBean;
import com.avcdata.aoscustommade.bean.VerifyPhotoNumberBean;
import com.avcdata.aoscustommade.main.BaseCallBack;
import com.avcdata.aoscustommade.main.LoginBaseCallBack;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.TextUitl;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;
import com.avcdata.national.network.BaseLog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 登录相关的view
 * Created by zyh on 2017/6/5.
 */

public class LoginActivityModel {
    private final Context mContext;

    public LoginActivityModel(Context context) {
        this.mContext = context;
    }

    /**
     * 获取手机验证码
     * @param userAccount 手机号
     * @param baseCallBack 回调函数
     */
    public void loadCode(String userAccount, final BaseCallBack baseCallBack){
        JSONObject jsonObject = new JSONObject();
        String url = AvcConstants.URLHeaderUserinfo;
        try {
            if (TextUitl.isPhone(userAccount)) {
                jsonObject.put("telephone", userAccount);
                url = url + "sendVerifyNum";
            } else {
                jsonObject.put("email", userAccount);
                url = url + "sendVerifyByMail";
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        AvcHttpUtils.post(url, jsonObject, false, new AvcJsonHttpResponseHandler(mContext, GetVerificationCodeBean.class){
            @Override
            public void onStart() {
                super.onStart();

            }

            @Override
            public void onSuccess(Object object) {
                GetVerificationCodeBean bean = (GetVerificationCodeBean) object;
                boolean success = bean.isSuccess();
                baseCallBack.onSuccess(success);
                super.onSuccess(object);

            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }
        });
    }

    /**
     * 校验手机号
     * @param userAccount 手机号
     * @param baseCallBack 回调函数
     */
    public void loadCheackAccount(String userAccount, final BaseCallBack baseCallBack) {
        String url = AvcConstants.URLHeaderUserinfo;
        if (TextUitl.isPhone(userAccount)) {
            url = url + "isExists?type=username&keyWord="+ userAccount;
        } else {
            url = url + "isExists?type=email&keyWord="+ userAccount;
        }
        AvcHttpUtils.get(url, new AvcJsonHttpResponseHandler(mContext, VerifyPhotoNumberBean.class){
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                VerifyPhotoNumberBean bean = (VerifyPhotoNumberBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }
        });

    }

    /**
     * 获取手机key
     * @param account
     * @param baseCallBack
     */
    public void loadGetSecreKey(String account, final LoginBaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("username", account);
        } catch (JSONException e){
            e.printStackTrace();
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "secretKeyIsExists", jsonObject, false, new AvcJsonHttpResponseHandler(mContext, GetVerificationCodeBean.class){
            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                GetVerificationCodeBean  bean = (GetVerificationCodeBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }

            @Override
            public void onFailure(BaseResponseBean baseResponseBean) {
                super.onFailure(baseResponseBean);
                baseCallBack.onFailure(baseResponseBean);
            }
        });

    }

    /**
     * 验证手机验证码
     * @param telephone 手机号
     * @param userTextCode 验证码
     * @param baseCallBack
     */
    public void loadCheckCode(String telephone, String userTextCode, final BaseCallBack baseCallBack) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone", telephone);
            jsonObject.put("code", userTextCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "ifVerifyPass", jsonObject, new AvcJsonHttpResponseHandler(mContext, VerifyNoteBean.class){

            @Override
            public void onSuccess(Object object) {
                super.onSuccess(object);
                VerifyNoteBean bean = (VerifyNoteBean) object;
                baseCallBack.onSuccess(bean);
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
            }
        });

    }

    /**
     * 登录
     * @param userName
     * @param passWord
     * @param key
     * @param baseCallBack
     */
    public void loadLogin(String userName, String passWord, String key, final BaseCallBack baseCallBack) {
        JSONObject requestJsonObject = new JSONObject();
        try {
            requestJsonObject.put("username", userName);
            requestJsonObject.put("password", passWord);
            requestJsonObject.put("secretKey", key);
            BaseLog.e("login","this is login try");
        } catch (JSONException e) {
            e.printStackTrace();
            BaseLog.e("login","this is login catch");
        }
        AvcHttpUtils.post(AvcConstants.URLHeaderUserinfo + "login", requestJsonObject, new AvcJsonHttpResponseHandler(mContext){
            @Override
            public void onSuccess(JSONObject jsonobject) {
                super.onSuccess(jsonobject);
                JSONObject jsonObject = jsonobject;
                baseCallBack.onSuccess(jsonObject);
                BaseLog.e("这是login","成功");
            }

            @Override
            public void onFailure() {
                super.onFailure();
                baseCallBack.onFailure();
                BaseLog.e("login","login failed");
            }
        });
    }
}
